package com.orange.core.config.handler;

import com.orange.core.exception.DuplicateException;
import com.orange.core.pojo.R;
import com.orange.core.pojo.enums.EErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.TypeMismatchException;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.stream.Collectors;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler({AccessDeniedException.class})
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public R<Object> exceptionHandler(AccessDeniedException e) {
        log.error(e.getMessage(), e);
        return R.fail(EErrorCode.ACCESS_FORBIDDEN);
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    public R<Object> exceptionHandler(HttpRequestMethodNotSupportedException e) {
        log.error(e.getMessage(), e);
        return R.fail(EErrorCode.METHOD_NOT_ALLOWED, e.getMessage());
    }

    @ExceptionHandler({BindException.class})
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public R<Object> exceptionHandler(BindException e) {
        log.error(e.getMessage(), e);
        BindingResult bindingResult = e.getBindingResult();
        String message = bindingResult.hasErrors()
                ? bindingResult.getFieldErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining("\r\n"))
                : "未知错误";
        return R.fail(EErrorCode.PARAM_ERROR, message);
    }

    @ExceptionHandler({ConstraintViolationException.class})
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public R<Object> exceptionHandler(ConstraintViolationException e) {
        log.error(e.getMessage(), e);
        String message = e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining("\r\n"));
        return R.fail(EErrorCode.PARAM_ERROR, message);
    }

    @ExceptionHandler(TypeMismatchException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public R<Object> exceptionHandler(TypeMismatchException e) {
        log.error(e.getMessage(), e);
        return R.fail(EErrorCode.PARAM_ERROR);
    }

    @ExceptionHandler(HttpMessageConversionException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public R<Object> exceptionHandler(HttpMessageConversionException e) {
        log.error(e.getMessage(), e);
        return R.fail(EErrorCode.PARAM_ERROR);
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public R<Object> exceptionHandler(NoHandlerFoundException e) {
        log.error(e.getMessage(), e);
        return R.fail(EErrorCode.NOT_FOUND);
    }

    @ExceptionHandler(DuplicateKeyException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public R<Object> exceptionHandler(DuplicateKeyException e) {
        log.error(e.getMessage(), e);
        return R.fail(EErrorCode.PARAM_ERROR, "保存失败，请检查数据是否已存在");
    }

    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public R<Object> exceptionHandler(IllegalArgumentException e) {
        log.error(e.getMessage(), e);
        return R.fail(EErrorCode.PARAM_ERROR, e.getMessage());
    }

    @ExceptionHandler(IllegalStateException.class)
    @ResponseStatus(HttpStatus.PRECONDITION_FAILED)
    public R<Object> exceptionHandler(IllegalStateException e) {
        log.error(e.getMessage(), e);
        return R.fail(EErrorCode.SYSTEM_ERROR, e.getMessage());
    }

    @ExceptionHandler(DuplicateException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public R<Object> exceptionHandler(DuplicateException e) {
        log.error(e.getMessage(), e);
        return R.fail(EErrorCode.DUPLICATE_EXCHANGE_CAR_COUPON, e.getMessage());
    }

    @ExceptionHandler({Exception.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public R<Object> exceptionHandler(Exception e) {
        log.error(e.getMessage(), e);
        return R.fail(EErrorCode.SYSTEM_ERROR);
    }
}
